import Vue from 'vue'
import VueI18n from 'vue-i18n'
import zhCN from './locales/zh-CN'
import enUS from './locales/en-US'

Vue.use(VueI18n)

const messages = {
  'zh-CN': zhCN,
  'en-US': enUS
}

// 获取浏览器语言设置
function getDefaultLocale() {
  const locale = localStorage.getItem('locale')
  if (locale) {
    return locale
  }
  
  const browserLocale = navigator.language || navigator.userLanguage
  if (browserLocale.startsWith('en')) {
    return 'en-US'
  }
  return 'zh-CN'
}

const i18n = new VueI18n({
  locale: getDefaultLocale(),
  fallbackLocale: 'zh-CN',
  messages
})

export default i18n
